<!--
 * @description 小组活动列表管理
 * @author dumeng
 * @date 2021/9/17
 -->
<template>
  <section class="group-activity-list" :class="[groupId && 'pt0']">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.name"
          placeholder="输入活动名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </section>
      </section>
    </el-collapse-transition>
    <el-collapse-transition>
      <section v-show="!searchshow" class="main-search separate">
        <section class="search-inner">
          <el-row>
            <el-col :span="12">
              <section class="search-inner-item">
                <div class="item-label">活动名称</div>
                <el-input
                  v-model.trim="search.name"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入活动名称搜索"
                />
              </section>
            </el-col>
            <el-col :span="12">
              <section class="search-inner-item">
                <div class="item-label">小组名称</div>
                <el-input
                  v-model.trim="search.groupName"
                  style="width: 90%;"
                  class="filter-item"
                  placeholder="请输入小组搜索"
                />
              </section>
            </el-col>

          </el-row>
        </section>
        <section class="search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <span class="search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>活动管理</span>
      </section>
    </section>
    <div class="classify-tab">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane v-for="item in checkList" :key="item.id" :label="item.name" :name="item.id" />
      </el-tabs>
    </div>
    <section class="main-table">
      <v-table
        :list="list"
        tag="groupActivityList"
        :source-arr="['activityImg']"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="false"
      >
        <el-table-column label="操作" fixed="right" align="center" min-width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.groupStatus!=='DISBANDED'" type="text" size="mini" @click="handleStatus(scope.row)">{{ scope.row.activityStatus==='ZT01' ? '禁用' : '启用' }}</el-button>
            <el-button type="text" size="mini" @click="handleView(scope.row)">详情</el-button>
            <el-button
              type="text"
              size="mini"
              @click="toApplyRecord(scope.row)"
            >报名
            </el-button>

            <el-button type="text" size="mini" @click="toSignList(scope.row)">签到</el-button>
            <el-button
              type="text"
              size="mini"
              @click="toTidbitsList(scope.row)"
            >花絮
            </el-button>
            <el-button v-if="scope.row.activityStatus==='ZT01'" type="text" size="mini" @click="handleRecommend(scope.row)">{{ scope.row.isRecommend === '1' ? '取消推荐' : '推荐' }}</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import { queryPage, updateRecommend, updateStatus } from '../api/activity'
import permission from '@/directive/permission'
import { mapGetters } from 'vuex'

export default {
  name: 'GroupActivityList',
  components: { Pagination, VTable },
  directives: { permission },
  props: {
    groupId: {
      type: String,
      default: ''
    },
    gi: {
      type: String,
      default: ''
    },
    ci: {
      type: String,
      default: ''
    },
    gn: {
      type: String,
      default: ''
    },
    tab: {
      type: String,
      default: ''
    },
    amTab: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      searchshow: true,
      search: {
        name: '',
        groupName: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      view: '/API-DOCUMENT/document/file/download?id=',
      activeTab: '1',
      checkList: [
        {
          name: '全部',
          id: '1'
        },
        {
          name: '推荐',
          id: '2'
        }
      ]
    }
  },
  watch: {
    activeTab(val) {
      if (this.groupId) {
        const obj = { path: this.$filterPath('groupManage'), query: { gi: this.gi,
          tab: this.tab, ci: this.ci, gn: this.gn, amTab: val }}
        this.$store.dispatch('changeBackUrl', obj)
      }
    }
  },
  computed: {
    ...mapGetters([
      'backUrl'
    ])
  },
  created() {
    if (!this.groupId) {
      this.$store.dispatch('changeBackUrl', null)
    } else {
      const obj = { path: this.$filterPath('groupManage'), query: { gi: this.gi,
        tab: this.tab, ci: this.ci, gn: this.gn, amTab: this.amTab }}
      this.$store.dispatch('changeBackUrl', obj)
    }
    if (this.groupId && this.amTab) {
      this.activeTab = this.amTab
    } else if (this.$route.query.t) {
      this.activeTab = this.$route.query.t
    }

    this.getList()
  },
  methods: {
    handleStatus(row) {
      this.$confirm(row.activityStatus === 'ZT01' ? '确认禁用？' : '确认启用？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateStatus({ id: row.id, groupId: row.groupId, activityStatus: row.activityStatus === 'ZT01' ? 'ZT02' : 'ZT01' }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg || '操作失败!'
            })
          }
        })
      }).catch(() => {

      })
    },
    handleRecommend(row) {
      this.$confirm(row.isRecommend === '1' ? '确认取消推荐？' : '确认推荐？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateRecommend({ id: row.id, groupId: row.groupId, isRecommend: row.isRecommend === '1' ? '0' : '1' }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: res.data.msg
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    handleTabClick() {
      this.listQuery.page = 1
      this.getList()
    },

    /**
     * 查看详情
     */
    handleView(row) {
      // if (this.groupId) {
      this.$router.push({ path: this.$filterPath('activityDetail'), query: { i: row.id, t: this.activeTab }})
      // } else {
      // this.$router.push()
      // }
    },

    /**
     * 报名详情
     */
    toApplyRecord(row) {
      this.$router.push({ path: this.$filterPath('applyRecord'), query: { ai: row.id, gi: this.groupId, t: this.activeTab }})
    },
    /**
     * 活动花絮
     */
    toTidbitsList(row) {
      this.$router.push({ path: this.$filterPath('tidbitsList'), query: { i: row.id, gi: row.groupId, t: this.activeTab }})
    },

    /**
     * 签到记录
     * @param id
     */
    toSignList(row) {
      // this.$router.push({ path: this.$filterPath('signRecord'), query: { i: row.id }})
      this.$router.push({ path: this.$filterPath('signList'), query: { i: row.id, t: this.activeTab, gi: row.groupId }})
    },

    filtercolumn(scope, item) {
      if (item === 'activityImg') {
        return `${this.view + scope.row['activityImg']}`
      } else {
        return scope.row[item]
      }
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        groupId: this.groupId
      }
      if (this.searchshow) {
        query['activityName'] = this.listQuery.name
      } else {
        query['activityName'] = this.search.name
        query['groupName'] = this.search.groupName
      }
      const url = this.activeTab === '1' ? '/backend/interestGroupActivity/queryPage' : '/backend/interestGroupActivity/recommendActivityList'
      return new Promise((resolve, reject) => {
        queryPage(query, url).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">

.group-activity-list {
  padding: 10px;
  &.pt0{
    padding-top: 0;
  } .main-search {
      background-color: #fff;
      padding: 0 10px;

      .svg-container {
        padding: 0 5px 0 10px;
        color: #bfced6;
        vertical-align: middle;
        display: inline-block;
      }

      .search-input {
        width: 60%;
        position: relative;

        &:after {
          content: '';
          position: absolute;
          left: -25px;
          right: 0;
          bottom: 8px;
          height: 1px;
          background: rgba(226, 227, 232, 1);
        }

        input {
          outline: none;
          background: transparent;
          border: none;
          outline: medium;
          padding: 0;
          &:focus {
            outline: none;
            background-color: transparent;
          }
        }
      }

      .search-btn {
        display: inline-block;
        float: right;

        .el-button.is-round {
          padding: 10px 30px;
          margin-right: 34px;
        }

        .search-more {
          font-size: 14px;
          font-weight: 500;
          @include font-color($mainColor);
          cursor: pointer;
          margin-left: 10px;
        }
      }

      &.merge {
        height: 50px;
        line-height: 50px;
      }

      &.separate {
        .search-inner {
          padding-top: 10px;

          .search-inner-item {
            .item-label {
              height: 30px;
              line-height: 30px;
              font-size: 14px;
              color: #a6a6a6;
              font-weight: 400;
            }

            .el-input {
              position: relative;

              &.el-date-editor input {
                padding-left: 30px;
              }

              .el-input__inner {
                border: none;
                padding: 0;
              }

              &:after {
                content: '';
                height: 1px;
                background-color: #e2e3e8;
                position: absolute;
                left: 0;
                right: 0;
                bottom: 0;
              }
            }
          }
        }

        .search-btn {
          text-align: center;
          display: block;
          float: none;
          height: 56px;
          line-height: 56px;
        }
      }
    }
  .classify-tab {
    width: 100%;
    margin-bottom: 10px;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
